package com.iwunu.gateway.util;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

public class HuiDu {

    /**
     * 图片颜色的处理
     * @param alpha //透明名度
     * 三原色 
     * @param red   
     * @param green
     * @param blue  
     * @return
     */
    private static int colorToRGB(int alpha, int red, int green, int blue) {

        /************************************************************
         * 对于正数和负数在不考虑溢出的情况下，他们的运算如下：           *
         * 3 << 2，则是将数字3左移2位，解释如下：                      *
         * 左移一位都相当于乘以2的1次方，左移n位就相当于乘以2的n次方。 *
         * 即  newPixel << 8  即等于 newPixel * 2^8                     *
         * **********************************************************
         */
        int newPixel = 0;
        newPixel += alpha;
        newPixel = newPixel << 8;
        newPixel += red;
        newPixel = newPixel << 8;
        newPixel += green;
        newPixel = newPixel << 8;
        newPixel += blue;

        return newPixel;

    }
    public static void main(String[] args) throws IOException {
        BufferedImage bufferedImage 
            = ImageIO.read(new File(("D:/test/kinfeng11.jpg")));
        //获取到图片的属性信息
        BufferedImage grayImage = 
            new BufferedImage(bufferedImage.getWidth(), 
                          bufferedImage.getHeight(), 
                          bufferedImage.getType());
        //图片的宽高
        System.out.println("getWidth----->"+bufferedImage.getWidth());

        for (int i = 0; i < bufferedImage.getWidth(); i++) {
            for (int j = 0; j < bufferedImage.getHeight(); j++) {
                final int color = bufferedImage.getRGB(i, j);

                final int r = (color >> 16) & 0xff;     //右移四位
                final int g = (color >> 8) & 0xff;      //右移3为
                final int b = color & 0xff;
                //运用灰度处理 的方法  加权平均值
                int gray = (int) (0.3 * r + 0.59 * g + 0.11 * b);
                //每一个像素点的灰度转化
                int newPixel = colorToRGB(255, gray, gray, gray);
                grayImage.setRGB(i, j, newPixel);
            }
        }
        File newFile = new File(("D:/test/kinfenghd.jpg"));
        ImageIO.write(grayImage, "jpg", newFile);
    }

}
